﻿namespace WaveringLand_Launcher.Console
{
    using ProjBobcat.Class.Helper;
    using ProjBobcat.Class.Model;
    using ProjBobcat.Class.Model.LauncherProfile;
    using ProjBobcat.DefaultComponent;
    using ProjBobcat.DefaultComponent.Authenticator;
    using ProjBobcat.DefaultComponent.Launch;
    using ProjBobcat.DefaultComponent.ResourceInfoResolver;
    using ProjBobcat.Event;
    using ProjBobcat.Interface;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Threading.Tasks;

    class Program
    {
        static async Task Main(string[] args)
        {
            ServicePointManager.DefaultConnectionLimit = 512;
            Console.WriteLine("Hello World!");
            ServiceHelper.Init();
            HttpClientHelper.Init();

            //
            var javaList = ProjBobcat.Class.Helper.SystemInfoHelper.FindJava(); // Returns a list of all java installations found in registry.
            Console.WriteLine(string.Join("\n", javaList.ToList()));


            var rootPath = ".minecraft";
            var clientToken = Guid.NewGuid();
            var core = new DefaultGameCore
            {
                ClientToken = clientToken, // Game's identifier, set it to any GUID you like, such as 88888888-8888-8888-8888-888888888888 or a randomly generated one.
                RootPath = rootPath, // Path of .minecraft\, you had better use absolute path.
                VersionLocator = new DefaultVersionLocator(rootPath, clientToken)
                {
                    LauncherProfileParser = new DefaultLauncherProfileParser(rootPath, clientToken)
                }
            };
            var gameList = core.VersionLocator.GetAllGames().ToList();
            //
            var launchSettings = new LaunchSettings
            {
                FallBackGameArguments = new GameArguments // Default game arguments for all games in .minecraft/ as the fallback of specific game launch.
                {
                    GcType = GcType.G1Gc, // GC type
                    JavaExecutable = javaList.First(), //The path of Java executable
                    Resolution = new ResolutionModel // Game Window's Resolution
                    {
                        Height = 600, // Height
                        Width = 800 // Width
                    },
                    MinMemory = 1024, // Minimal Memory
                    MaxMemory = 2048 // Maximum Memory
                },
                Version = core.VersionLocator.GetAllGames().First()?.Id, // The version ID of the game to launch, such as 1.7.10 or 1.15.2
                VersionInsulation = false, // Version Isolation
                GameResourcePath = core.RootPath, // Root path of the game resource(.minecraft/)
                GamePath = core.RootPath, // Root path of the game (.minecraft/versions/)
                VersionLocator = core.VersionLocator, // Game's version locator
                GameName = gameList[0].Name,
                LauncherName = "WLL"
            };
            launchSettings.WindowTitle = "WaveringLand2.0 —— 魔法与科技!!!!";
            launchSettings.Authenticator = new OfflineAuthenticator
            {
                
                Username = "Username",
                LauncherAccountParser = core.VersionLocator.LauncherAccountParser??new DefaultLauncherAccountParser(rootPath,clientToken) // launcher_profiles.json parser
            };
            //Console.WriteLine(core.LaunchArgumentParser.GenerateLaunchArguments());
            core.GameLogEventDelegate += delegate (object sender, GameLogEventArgs e)
            {
                Console.WriteLine(e.Content);
            };
            core.LaunchLogEventDelegate += delegate (object sender, LaunchLogEventArgs e)
              {
                  Console.WriteLine(e.Item);

              };

            var result = await core.LaunchTaskAsync(launchSettings).ConfigureAwait(true); // Returns the launch result
            Console.WriteLine(result.Error);
            Console.ReadKey();
        }
    }
}
